package org.dragon.commons;

/**
 * Author:     Zhao Yan
 * DateTime:   2023/5/17 10:08
 */
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;

import java.util.List;
import java.util.stream.Collectors;

public enum Errors {

    Unknown(-1, "未知错误"),
    NotLogin(-2, "用户未登录"),
    AuthFailed(-3, "认证失败"),
    BadRequest(-4, "错误的请求"),
    NotPermit(-5, "没有权限"),
    InvalidCredential(-6, "无效的凭证"),
    CodeExpired(-7, "验证码已过期"),
    OperateTooFrequent(-8, "操作过于频繁"),
    InvalidOperation(-9, "非法操作"),
    TokenExpired(-10, "授权已超时，请重新登录"),
    InvalidEmailCode(-11, "非法的邮箱验证码"),
    DuplicateKey(-12, "重复的Key"),
    ;

    public final int code;

    public final String message;

    Errors(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static List<String> getErrors(BindException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        return allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.toList());
    }
}